

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Fly542">
  <meta name="keywords" content="">
  
    <meta name="description" content="lsof（list open files）是一个查看当前系统文件的工具。在linux环境下，任何事物都以文件的形式存在，通过文件不仅仅可以访问常规数据，还可以访问网络连接和硬件。如传输控制协议 (TCP) 和用户数据报协议 (UDP) 套接字等，系统在后台都为该应用程序分配了一个文件描述符，该文件描述符提供了大量关于这个应用程序本身的信息。 lsof打开的文件可以是：  普通文件 目录 网络文件系">
<meta property="og:type" content="article">
<meta property="og:title" content="lsof 一切皆文件">
<meta property="og:url" content="http://fly542.cn/2020/08/20/03%E8%BD%AF%E4%BB%B6%E5%BC%80%E5%8F%91/03linux/04lsof%E4%B8%80%E5%88%87%E7%9A%86%E6%96%87%E4%BB%B6/index.html">
<meta property="og:site_name" content="Fly542 技术沉淀">
<meta property="og:description" content="lsof（list open files）是一个查看当前系统文件的工具。在linux环境下，任何事物都以文件的形式存在，通过文件不仅仅可以访问常规数据，还可以访问网络连接和硬件。如传输控制协议 (TCP) 和用户数据报协议 (UDP) 套接字等，系统在后台都为该应用程序分配了一个文件描述符，该文件描述符提供了大量关于这个应用程序本身的信息。 lsof打开的文件可以是：  普通文件 目录 网络文件系">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-08-20T04:25:11.000Z">
<meta property="article:modified_time" content="2022-12-30T03:33:01.394Z">
<meta property="article:author" content="Fly542">
<meta property="article:tag" content="linux">
<meta property="article:tag" content="tools">
<meta name="twitter:card" content="summary_large_image">
  
  
  <title>lsof 一切皆文件 - Fly542 技术沉淀</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"fly542.cn","root":"/","version":"1.8.14","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 6.0.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Fly542 技术沉淀</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="lsof 一切皆文件">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2020-08-20 12:25" pubdate>
        2020年8月20日 中午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      3.2k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      27 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">lsof 一切皆文件</h1>
            
            <div class="markdown-body">
              <p>lsof（list open files）是一个查看当前系统文件的工具。在linux环境下，任何事物都以文件的形式存在，通过文件不仅仅可以访问常规数据，还可以访问网络连接和硬件。如传输控制协议 (TCP) 和用户数据报协议 (UDP) 套接字等，系统在后台都为该应用程序分配了一个文件描述符，该文件描述符提供了大量关于这个应用程序本身的信息。</p>
<p>lsof打开的文件可以是：</p>
<ol>
<li>普通文件</li>
<li>目录</li>
<li>网络文件系统的文件</li>
<li>字符或设备文件</li>
<li>(函数)共享库</li>
<li>管道，命名管道</li>
<li>符号链接</li>
<li>网络文件（例如：NFS file、网络socket，unix域名socket）</li>
<li>还有其它类型的文件，等等</li>
</ol>
<h1 id="命令参数"><a href="#命令参数" class="headerlink" title="命令参数"></a>命令参数</h1><ul>
<li>-a 列出打开文件存在的进程</li>
<li>-c&lt;进程名&gt; 列出指定进程所打开的文件</li>
<li>-g 列出GID号进程详情</li>
<li>-d&lt;文件号&gt; 列出占用该文件号的进程</li>
<li>+d&lt;目录&gt; 列出目录下被打开的文件</li>
<li>+D&lt;目录&gt; 递归列出目录下被打开的文件</li>
<li>-n&lt;目录&gt; 列出使用NFS的文件</li>
<li>-i&lt;条件&gt; 列出符合条件的进程。（4、6、协议、:端口、 @ip ）</li>
<li>-p&lt;进程号&gt; 列出指定进程号所打开的文件</li>
<li>-u 列出UID号进程详情</li>
<li>-h 显示帮助信息</li>
<li>-v 显示版本信息</li>
</ul>
<h1 id="使用示例"><a href="#使用示例" class="headerlink" title="使用示例"></a>使用示例</h1><h2 id="1：无任何参数"><a href="#1：无任何参数" class="headerlink" title="1：无任何参数"></a>1：无任何参数</h2><figure class="highlight tap"><table><tr><td class="gutter"><div class="code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></div></td><td class="code"><pre><code class="hljs tap">$lsof| more<br>COMMAND     PID      USER   FD      TYPE             DEVICE SIZE/OFF       NODE NAME<br>init         <span class="hljs-number"> 1 </span>     root  cwd       DIR              253,0    <span class="hljs-number"> 4096 </span>        <span class="hljs-number"> 2 </span>/<br>init         <span class="hljs-number"> 1 </span>     root  rtd       DIR              253,0    <span class="hljs-number"> 4096 </span>        <span class="hljs-number"> 2 </span>/<br>init         <span class="hljs-number"> 1 </span>     root  txt       REG              253,0  <span class="hljs-number"> 150352 </span>  <span class="hljs-number"> 1310795 </span>/sbin/init<br></code></pre></td></tr></table></figure>
<p>说明：</p>
<p>lsof输出各列信息的意义如下：</p>
<ul>
<li>COMMAND：进程的名称</li>
<li>PID：进程标识符</li>
<li>PPID：父进程标识符（需要指定-R参数）</li>
<li>USER：进程所有者</li>
<li>PGID：进程所属组</li>
<li>FD：文件描述符，应用程序通过文件描述符识别该文件。如cwd、txt等::<figure class="highlight pgsql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs pgsql">（<span class="hljs-number">1</span>）cwd：表示<span class="hljs-keyword">current</span> <span class="hljs-keyword">work</span> dirctory，即：应用程序的当前工作目录，这是该应用程序启动的目录，除非它本身对这个目录进行更改<br>   （<span class="hljs-number">2</span>）txt ：该类型的文件是程序代码，如应用程序二进制文件本身或共享库，如上列表中显示的 /sbin/init 程序<br>   （<span class="hljs-number">3</span>）lnn：library <span class="hljs-keyword">references</span> (AIX);<br>   （<span class="hljs-number">4</span>）er：FD information error (see <span class="hljs-type">NAME</span> <span class="hljs-keyword">column</span>);<br>   （<span class="hljs-number">5</span>）jld：jail directory (FreeBSD);<br>   （<span class="hljs-number">6</span>）ltx：shared library text (code <span class="hljs-keyword">and</span> data);<br>   （<span class="hljs-number">7</span>）mxx ：hex memory-mapped <span class="hljs-keyword">type</span> number xx.<br>   （<span class="hljs-number">8</span>）m86：DOS Merge mapped file;<br>   （<span class="hljs-number">9</span>）mem：memory-mapped file;<br>   （<span class="hljs-number">10</span>）mmap：memory-mapped device;<br>   （<span class="hljs-number">11</span>）pd：parent directory;<br>   （<span class="hljs-number">12</span>）rtd：root directory;<br>   （<span class="hljs-number">13</span>）tr：kernel trace file (OpenBSD);<br>   （<span class="hljs-number">14</span>）v86  VP/ix mapped file;<br>   （<span class="hljs-number">15</span>）<span class="hljs-number">0</span>：表示标准输入<br>   （<span class="hljs-number">16</span>）<span class="hljs-number">1</span>：表示标准输出<br>   （<span class="hljs-number">17</span>）<span class="hljs-number">2</span>：表示标准错误<br>   一般在标准输出、标准错误、标准输入后还跟着文件状态模式：r、w、u等<br>   （<span class="hljs-number">1</span>）u：表示该文件被打开并处于读取/写入模式<br>   （<span class="hljs-number">2</span>）r：表示该文件被打开并处于只读模式<br>   （<span class="hljs-number">3</span>）w：表示该文件被打开并处于<br>   （<span class="hljs-number">4</span>）空格：表示该文件的状态模式为unknow，且没有锁定<br>   （<span class="hljs-number">5</span>）-：表示该文件的状态模式为unknow，且被锁定<br>   同时在文件状态模式后面，还跟着相关的锁<br>   （<span class="hljs-number">1</span>）N：<span class="hljs-keyword">for</span> a Solaris NFS <span class="hljs-keyword">lock</span> <span class="hljs-keyword">of</span> <span class="hljs-type">unknown</span> <span class="hljs-keyword">type</span>;<br>   （<span class="hljs-number">2</span>）r：<span class="hljs-keyword">for</span> <span class="hljs-keyword">read</span> <span class="hljs-keyword">lock</span> <span class="hljs-keyword">on</span> part <span class="hljs-keyword">of</span> the file;<br>   （<span class="hljs-number">3</span>）R：<span class="hljs-keyword">for</span> a <span class="hljs-keyword">read</span> <span class="hljs-keyword">lock</span> <span class="hljs-keyword">on</span> the entire file;<br>   （<span class="hljs-number">4</span>）w：<span class="hljs-keyword">for</span> a <span class="hljs-keyword">write</span> <span class="hljs-keyword">lock</span> <span class="hljs-keyword">on</span> part <span class="hljs-keyword">of</span> the file;（文件的部分写锁）<br>   （<span class="hljs-number">5</span>）W：<span class="hljs-keyword">for</span> a <span class="hljs-keyword">write</span> <span class="hljs-keyword">lock</span> <span class="hljs-keyword">on</span> the entire file;（整个文件的写锁）<br>   （<span class="hljs-number">6</span>）u：<span class="hljs-keyword">for</span> a <span class="hljs-keyword">read</span> <span class="hljs-keyword">and</span> <span class="hljs-keyword">write</span> <span class="hljs-keyword">lock</span> <span class="hljs-keyword">of</span> <span class="hljs-keyword">any</span> length;<br>   （<span class="hljs-number">7</span>）U：<span class="hljs-keyword">for</span> a <span class="hljs-keyword">lock</span> <span class="hljs-keyword">of</span> <span class="hljs-type">unknown</span> <span class="hljs-keyword">type</span>;<br>   （<span class="hljs-number">8</span>）x：<span class="hljs-keyword">for</span> an SCO OpenServer Xenix <span class="hljs-keyword">lock</span> <span class="hljs-keyword">on</span> part      <span class="hljs-keyword">of</span> the file;<br>   （<span class="hljs-number">9</span>）X：<span class="hljs-keyword">for</span> an SCO OpenServer Xenix <span class="hljs-keyword">lock</span> <span class="hljs-keyword">on</span> the      entire file;<br>   （<span class="hljs-number">10</span>）space：<span class="hljs-keyword">if</span> there <span class="hljs-keyword">is</span> <span class="hljs-keyword">no</span> <span class="hljs-keyword">lock</span>.<br></code></pre></td></tr></table></figure></li>
<li>TYPE：文件类型，如DIR、REG等，常见的文件类型::<figure class="highlight less"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs less">（<span class="hljs-selector-tag">1</span>）<span class="hljs-selector-tag">DIR</span>：表示目录<br>（<span class="hljs-selector-tag">2</span>）<span class="hljs-selector-tag">CHR</span>：表示字符类型<br>（<span class="hljs-selector-tag">3</span>）<span class="hljs-selector-tag">BLK</span>：块设备类型<br>（<span class="hljs-selector-tag">4</span>）<span class="hljs-selector-tag">UNIX</span>： <span class="hljs-selector-tag">UNIX</span> 域套接字<br>（<span class="hljs-selector-tag">5</span>）<span class="hljs-selector-tag">FIFO</span>：先进先出 (FIFO) 队列<br>（<span class="hljs-selector-tag">6</span>）<span class="hljs-selector-tag">IPv4</span>：网际协议 (IP) 套接字<br></code></pre></td></tr></table></figure></li>
<li>DEVICE：指定磁盘的名称</li>
<li>SIZE：文件的大小</li>
<li>NODE：索引节点（文件在磁盘上的标识）</li>
<li>NAME：打开文件的确切名称</li>
</ul>
<h2 id="2：查找某个文件相关的进程"><a href="#2：查找某个文件相关的进程" class="headerlink" title="2：查找某个文件相关的进程"></a>2：查找某个文件相关的进程</h2><figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs awk"><span class="hljs-variable">$lsof</span> <span class="hljs-regexp">/bin/</span>bash<br>COMMAND     PID USER  FD   TYPE DEVICE SIZE/OFF    NODE NAME<br>mysqld_sa  <span class="hljs-number">2169</span> root txt    REG  <span class="hljs-number">253</span>,<span class="hljs-number">0</span>   <span class="hljs-number">938736</span> <span class="hljs-number">4587562</span> <span class="hljs-regexp">/bin/</span>bash<br>ksmtuned   <span class="hljs-number">2334</span> root txt    REG  <span class="hljs-number">253</span>,<span class="hljs-number">0</span>   <span class="hljs-number">938736</span> <span class="hljs-number">4587562</span> <span class="hljs-regexp">/bin/</span>bash<br>bash      <span class="hljs-number">20121</span> root txt    REG  <span class="hljs-number">253</span>,<span class="hljs-number">0</span>   <span class="hljs-number">938736</span> <span class="hljs-number">4587562</span> <span class="hljs-regexp">/bin/</span>bash<br></code></pre></td></tr></table></figure>

<h2 id="3：列出某个用户打开的文件信息"><a href="#3：列出某个用户打开的文件信息" class="headerlink" title="3：列出某个用户打开的文件信息"></a>3：列出某个用户打开的文件信息</h2><figure class="highlight gams"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs gams"><span class="hljs-meta"><span class="hljs-meta-keyword">$lsof</span> -u username</span><br></code></pre></td></tr></table></figure>
<p> -u 选项，u是user的缩写</p>
<h2 id="4：列出某个程序进程所打开的文件信息"><a href="#4：列出某个程序进程所打开的文件信息" class="headerlink" title="4：列出某个程序进程所打开的文件信息"></a>4：列出某个程序进程所打开的文件信息</h2><figure class="highlight gams"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs gams"><span class="hljs-meta"><span class="hljs-meta-keyword">$lsof</span> -c mysql</span><br></code></pre></td></tr></table></figure>
<p>-c 选项将会列出所有以mysql这个进程开头的程序的文件，其实你也可以写成 lsof | grep mysql, 但是第一种方法明显比第二种方法要少打几个字符；</p>
<h2 id="5：列出某个用户以及某个进程所打开的文件信息"><a href="#5：列出某个用户以及某个进程所打开的文件信息" class="headerlink" title="5：列出某个用户以及某个进程所打开的文件信息"></a>5：列出某个用户以及某个进程所打开的文件信息</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash"><span class="hljs-variable">$lsof</span>  -u <span class="hljs-built_in">test</span> -c mysql<br></code></pre></td></tr></table></figure>
<h2 id="6：通过某个进程号显示该进程打开的文件"><a href="#6：通过某个进程号显示该进程打开的文件" class="headerlink" title="6：通过某个进程号显示该进程打开的文件"></a>6：通过某个进程号显示该进程打开的文件</h2><figure class="highlight gams"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs gams"><span class="hljs-meta"><span class="hljs-meta-keyword">$lsof</span> -p 11968</span><br></code></pre></td></tr></table></figure>
<h2 id="7：列出所有的网络连接"><a href="#7：列出所有的网络连接" class="headerlink" title="7：列出所有的网络连接"></a>7：列出所有的网络连接</h2><figure class="highlight gams"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs gams"><span class="hljs-meta"><span class="hljs-meta-keyword">$lsof</span> -i</span><br></code></pre></td></tr></table></figure>
<h2 id="8：列出所有tcp-网络连接信息"><a href="#8：列出所有tcp-网络连接信息" class="headerlink" title="8：列出所有tcp 网络连接信息"></a>8：列出所有tcp 网络连接信息</h2><figure class="highlight crmsh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs crmsh">$lsof -i tcp<br><br>$lsof -n -i tcp<br>COMMAND     PID  <span class="hljs-keyword">USER</span>   <span class="hljs-title">FD</span>   <span class="hljs-keyword">TYPE</span>  DEVICE SIZE/OFF <span class="hljs-keyword">NODE</span> <span class="hljs-title">NAME</span><br>svnserve  <span class="hljs-number">11552</span> weber    <span class="hljs-number">3</span>u  IPv4 <span class="hljs-number">3799399</span>      <span class="hljs-number">0</span>t0  TCP *:svn (LISTEN)<br>redis-ser <span class="hljs-number">25501</span> weber    <span class="hljs-number">4</span>u  IPv4  <span class="hljs-number">113150</span>      <span class="hljs-number">0</span>t0  TCP <span class="hljs-number">127.0</span>.<span class="hljs-number">0.1</span>:<span class="hljs-number">6379</span> (LISTEN)<br></code></pre></td></tr></table></figure>
<h2 id="9：列出谁在使用某个端口"><a href="#9：列出谁在使用某个端口" class="headerlink" title="9：列出谁在使用某个端口"></a>9：列出谁在使用某个端口</h2><figure class="highlight gams"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs gams"><span class="hljs-meta"><span class="hljs-meta-keyword">$lsof</span> -i :3306</span><br></code></pre></td></tr></table></figure>
<h2 id="10：列出某个用户的所有活跃的网络端口"><a href="#10：列出某个用户的所有活跃的网络端口" class="headerlink" title="10：列出某个用户的所有活跃的网络端口"></a>10：列出某个用户的所有活跃的网络端口</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash"><span class="hljs-variable">$lsof</span> -a -u <span class="hljs-built_in">test</span> -i<br></code></pre></td></tr></table></figure>

<h2 id="11：根据文件描述列出对应的文件信息"><a href="#11：根据文件描述列出对应的文件信息" class="headerlink" title="11：根据文件描述列出对应的文件信息"></a>11：根据文件描述列出对应的文件信息</h2><figure class="highlight gams"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs gams"><span class="hljs-meta"><span class="hljs-meta-keyword">$lsof</span> -d description(like 2)</span><br></code></pre></td></tr></table></figure>
<p>示例::</p>
<div class="code-wrapper"><pre><code class="hljs">$lsof -d 3 | grep PARSER1
tail      6499 tde    3r   REG    253,3   4514722     417798 /opt/applog/open/log/HOSTPARSER1_ERROR_141217.log.001
</code></pre></div>
<p>说明：<br>0表示标准输入，1表示标准输出，2表示标准错误，从而可知：所以大多数应用程序所打开的文件的 FD 都是从 3 开始</p>
<h2 id="12：列出被进程号为1234的进程所打开的所有IPV4-network-files"><a href="#12：列出被进程号为1234的进程所打开的所有IPV4-network-files" class="headerlink" title="12：列出被进程号为1234的进程所打开的所有IPV4 network files"></a>12：列出被进程号为1234的进程所打开的所有IPV4 network files</h2><figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs stylus"><span class="hljs-variable">$lsof</span> -<span class="hljs-selector-tag">i</span> <span class="hljs-number">4</span> -<span class="hljs-selector-tag">a</span> -<span class="hljs-selector-tag">p</span> <span class="hljs-number">1234</span><br></code></pre></td></tr></table></figure>
<h2 id="13：列出目前连接主机nf5260i5-td上端口为：20，21，80相关的所有文件信息，且每隔3秒重复执行"><a href="#13：列出目前连接主机nf5260i5-td上端口为：20，21，80相关的所有文件信息，且每隔3秒重复执行" class="headerlink" title="13：列出目前连接主机nf5260i5-td上端口为：20，21，80相关的所有文件信息，且每隔3秒重复执行"></a>13：列出目前连接主机nf5260i5-td上端口为：20，21，80相关的所有文件信息，且每隔3秒重复执行</h2><figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs apache"><span class="hljs-attribute">lsof</span> -i @nf<span class="hljs-number">5260</span>i<span class="hljs-number">5</span>-td:<span class="hljs-number">20</span>,<span class="hljs-number">21</span>,<span class="hljs-number">80</span> -r <span class="hljs-number">3</span><br></code></pre></td></tr></table></figure>
            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/%E8%BD%AF%E4%BB%B6%E5%BC%80%E5%8F%91/">软件开发</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/linux/">linux</a>
                    
                      <a class="hover-with-bg" href="/tags/tools/">tools</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2020/08/20/03%E8%BD%AF%E4%BB%B6%E5%BC%80%E5%8F%91/03linux/06strace%E8%B7%9F%E8%B8%AA%E8%BF%9B%E7%A8%8B%E4%B8%AD%E7%9A%84%E7%B3%BB%E7%BB%9F%E8%B0%83%E7%94%A8/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">strace 跟踪进程中的系统调用</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2020/08/20/03%E8%BD%AF%E4%BB%B6%E5%BC%80%E5%8F%91/06%E5%BC%80%E5%8F%91%E5%88%86%E6%9E%90/00objdump%20%E4%BA%8C%E8%BF%9B%E5%88%B6%E6%96%87%E4%BB%B6%E5%88%86%E6%9E%90/">
                        <span class="hidden-mobile">00 objdump 二进制文件分析</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
